# -*- coding: utf-8 -*-
"""
读写doc
"""

from docx import Document     # pip install python-docx
from docx.shared import Pt
from docx.oxml.ns import qn


# 读
def read_doc(path):
    try:
        document = Document(path)
        doctext = ''
        for paragraph in document.paragraphs:
            doctext += paragraph.text.strip()+'\n'
        return doctext
    except:
        return ''



# 写
def write_docx(filename, text):
    #打开文档
    document = Document()
    #加入不同等级的标题
    document.add_heading(u'一级标题',1)
    #添加文本
    paragraph = document.add_paragraph(text)
    #设置字号
    run = paragraph.add_run(u'设置字号、')
    run.font.size = Pt(24)
    
    #设置字体
    run = paragraph.add_run('Set Font,')
    run.font.name = 'Consolas'
    
    #设置中文字体
    run = paragraph.add_run(u'设置中文字体、')
    run.font.name=u'宋体'
    r = run._element
    r.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
        
    #保存文件
    document.save(filename)








